# 22_utas2009.R
# Purpose: Ideological Extremism and Political Participation in Japan
# Created: 2021-3-14 Taka-aki Asano
# Last Modified: 2021-10-10

# package
require("ltm")
require("haven")
require("dplyr")
require("tidyr")
require("psych")


# Voters
## read dataset
voter2009 <- read_sav(
  "http://www.masaki.j.u-tokyo.ac.jp/utas/2009_2010utas130816.sav", 
  user_na = FALSE
)

## specify variables
variables2009 <- c("Q013801", "Q013802", "Q013803", "Q013804", "Q013805", "Q013806", 
                   "Q013807", "Q013808", "Q013809", "Q013810", "Q013811", "Q013812",
                   "Q013813", "Q013814", "Q013815", "Q013816", "Q013817", "Q013818", 
                   "Q013819", "Q013820", "Q014001", "Q014002", "Q014003")

## handle missing data
voter2009 <- voter2009[rowSums(voter2009[,variables2009], na.rm = TRUE) != 0,]

## estimate item parameters
irt2009_voter <- ltm::grm(voter2009[,variables2009], IRT.param = FALSE)
irt2009_voter ## viewing

## estimate voters ideology
score2009 <- ltm::factor.scores(irt2009_voter, voter2009[,variables2009])
voter2009$Ideology <- -1 * score2009$score.dat$z1

## Median
median(voter2009$Ideology)
by(voter2009$Ideology, voter2009$Q010300, describe)
